using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Blog.Importer
{
  public partial class Form1 : Form
  {
    private const string boxTitle = "Blog Importer";

    public Form1()
    {
      InitializeComponent();
      CbImageOptions.SelectedItem = CbImageOptions.Items[0];
    }

    private void BtnOpenFile_Click(object sender, EventArgs e)
    {
      openFileDialog1.ShowDialog();
    }

    private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
      TxtSourcePath.Text = openFileDialog1.FileName;
    }

    private void BtnImport_Click(object sender, EventArgs e)
    {
      // Check Required Data
      string errorMsg = "";
      if (TxtSourcePath.Text == "")
        errorMsg += "Source Path required.\n";
      if (TxtBlogUrl.Text == "")
        errorMsg += "Blog Url required.\n";
      if (TxtLogin.Text == "")
        errorMsg += "Login required.\n";
      if (TxtPassword.Text == "")
        errorMsg += "Password required.\n";
      
      // If required fields in place, Validate Destination
      if (errorMsg != "")
      {
        // Validate Destination Call
        if (!BlogWriter.ValidateDestination(CleanPath(TxtBlogUrl.Text), TxtLogin.Text, TxtPassword.Text))
          errorMsg += "Blog url or login information incorrect.\n";
      }
      
      // Ok if error, show, otherwise Import!
      if (errorMsg != "")
        MessageBox.Show(errorMsg, boxTitle);
      else
      {
        if (cbSourceType.Text == "BlogML")
        {
          BlogML20Reader reader = new BlogML20Reader();
          try
          {
            reader.XmlReader = new XmlTextReader(TxtSourcePath.Text);
            reader.BlogPath = CleanPath(TxtBlogUrl.Text);
            reader.UserName = TxtLogin.Text;
            reader.Password = TxtPassword.Text;

            reader.RemoveDuplicates = chkRemoveDuplicates.Checked;
            if (!chkImportAuthor.Checked)
              reader.Author = TxtLogin.Text;

            reader.FileOption = CbImageOptions.Text;
            reader.FilePath = TxtFilePath.Text;

            reader.Import();
          }
          catch (System.Net.WebException ex)
          {
            if (ex.Status == System.Net.WebExceptionStatus.ProtocolError)
              MessageBox.Show("The specified URL does not exist", boxTitle);
            else
              MessageBox.Show("Could not connect to the specified URL", boxTitle);
          }
          catch (System.Web.Services.Protocols.SoapException ex)
          {
            MessageBox.Show("The username or password is incorrect.", boxTitle);
          }
          catch (Exception ex)
          {
            MessageBox.Show("Error: " + ex.Message, boxTitle);
          }
          
          // End Message
          MessageBox.Show("BlogML Import Complete", boxTitle);
        }
        else
        {
          //RSS Import
          RssReader reader = new RssReader();
          try
          {
            reader.RssPath = CleanPath(TxtSourcePath.Text);
            reader.BlogPath = CleanPath(TxtBlogUrl.Text);
            reader.UserName = TxtLogin.Text;
            reader.Password = TxtPassword.Text;

            reader.RemoveDuplicates = chkRemoveDuplicates.Checked;
            if (!chkImportAuthor.Checked)
              reader.Author = TxtLogin.Text;

            reader.FileOption = CbImageOptions.Text;
            reader.FilePath = TxtFilePath.Text;

            reader.Import();
          }
          catch (Exception ex)
          {
            MessageBox.Show("Error: " + ex.Message, boxTitle);
          }

          // End Message
          MessageBox.Show("RSS Import Complete.", boxTitle);
        }

      }

    }

    private void BtnSourceValidate_Click(object sender, EventArgs e)
    {
      if (cbSourceType.Text == "BlogML")
      {
        BlogML20Reader reader = new BlogML20Reader();
        try
        {
          reader.XmlReader = new XmlTextReader(TxtSourcePath.Text);
          reader.BlogPath = CleanPath(TxtBlogUrl.Text);
          reader.UserName = TxtLogin.Text;
          reader.Password = TxtPassword.Text;
          if (reader.Validate())
            MessageBox.Show("BlogML validates correctly.", boxTitle);
          else
            MessageBox.Show("BlogML invalid.", boxTitle);
        }
        catch 
        {
          MessageBox.Show("BlogML validation error.", boxTitle);
        }
      }
      else
      {
        RssReader reader = new RssReader();
        try
        {
          reader.RssPath = CleanPath(TxtSourcePath.Text);
          reader.BlogPath = CleanPath(TxtBlogUrl.Text);
          reader.UserName = TxtLogin.Text;
          reader.Password = TxtPassword.Text;

          if (reader.Validate())
            MessageBox.Show("RSS validates correctly.", boxTitle);
          else
            MessageBox.Show("RSS invalid.", boxTitle);
        }
        catch 
        {
          MessageBox.Show("RSS validation error.", boxTitle);
        }
      }
    }

    private void BtnDestValidate_Click(object sender, EventArgs e)
    {
      if (BlogWriter.ValidateDestination(CleanPath(TxtBlogUrl.Text), TxtLogin.Text, TxtPassword.Text))
        MessageBox.Show("Blog validates correctly.", boxTitle);
      else
        MessageBox.Show("Blog validation error.", boxTitle);
    }

    private string CleanPath(string path)
    {
      path = path.Trim();
      if (!path.EndsWith("/"))
        path += "/";
      return path;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void cbSourceType_SelectedValueChanged(object sender, EventArgs e)
    {
      if (cbSourceType.Text == "RSS")
      {
        BtnOpenFile.Enabled = false;
      }
      else
      {
        BtnOpenFile.Enabled = true;
      }
    }

  }
}